import axios from "axios";
import Nprogress from "nprogress";
import "nprogress/nprogress.css";

const ajax = axios.create({
  baseURL: "/api",
  timeout: 10000,
});

ajax.interceptors.request.use(function (config) {
  Nprogress.start();
  return config;
});

ajax.interceptors.response.use(
  function (response) {
    Nprogress.done();
    return response.data;
  },
  function (error) {
    Nprogress.done();
    if (error.response.status == 401) {
      alert("未授权");
    } else if (error.response.status == 403) {
      alert("禁止访问");
    } else if (error.response.status == 404) {
      alert("错误访问");
    } else if (error.response.status == 500) {
      alert("服务器出错了");
    }
    return new Promise(() => {});
  }
);

export default ajax;
